--funcion ( resource, numero de fotos en X, numero de fotos en Y ,
-- numero de fotos a coger ,desde que numero cojer )
--

local animation_tm = {}
animation_tm.__index = animation_tm
     
function animationNew( imagePath, xframe, yframe, num_frames_delay, num_frames_get , speed, time_max)
    local  anim = {}
    anim.img = imagePath 
    anim.speed = speed
    anim.frame = {}
    anim.xframe = anim.img:getWidth()/xframe
    anim.yframe = anim.img:getHeight()/yframe
    
    anim.num_max_frames = num_frames_get - num_frames_delay
    anim.x_p_f = xframe
    anim.y_p_f = yframe
    anim.n = 1
    anim.time_max = time_max
     anim.time = 0
    local x=0
    local y=0

    for i=1,num_frames_delay+num_frames_get do
    
        if (anim.xframe ==  x ) then
            x = 0
            y =  y + 1
        end

        if( i > num_frames_delay ) then
            local aquad = love.graphics.newQuad(  (x * anim.x_p_f ), (y * anim.y_p_f)  , anim.x_p_f , anim.y_p_f , anim.img:getWidth(), anim.img:getHeight())
            table.insert(anim.frame, aquad)
        end

        x = x + 1
    end
    return setmetatable(anim, animation_tm)
end


function animation_tm:draw(x, y, angle, sx, sy)

    love.graphics.draw(self.img, self.frame[ self.n ] , x, y, angle, sx, sy)

end

function animation_tm:update(dt)

    if  self.time > 1 then
        self.time = 0
        if self.n >= self.num_max_frames then
            self.n = 1
        else
            self.n = self.n + 1
        end
    else
        self.time = self.time + dt * self.speed
    end
end

function animation_tm:setSpeed( speed )
    self.speed = speed
end